<div class="row">	
	<!-- Add -->
	<div id="addModal" class="reveal-modal tiny" data-reveal>
		<p class="p-modal">Tambah Data Master Jabatan</p>
		<div class="isi-modal">
			<?php 
				echo form_open('m_jabatan/save', array('id' => 'SaveJabatan')); 
			?>
			<input type="text" name="jabatan" class="form-control form-elba input-sm clearable" placeholder="Tulis nama jabatan, misal: Kepala Tata Usaha"/><br/><br/>
			<span id="loadingAdd" style="display:none;">Sedang memproses..</span>
			<span id="errorAdd" style="display:none;color:red;">Ma'af, gagal menambahkan data</span>
			<span class="pull-right"><button type="submit" class="btn btn-xs tmb-utama"> Simpan</button> <button class="btn tmb-kedua close-reveal-modal close-modal"> Batal</button></span>
			<?php
				echo form_close();
			?>
		</div>
	</div>

	<!-- Edit -->
	<div id="editModal" class="reveal-modal tiny" data-reveal>
		<p class="p-modal">Edit Data Master Jabatan</p>
		<div class="isi-modal">			
			<div id="edit"></div>
		</div>
	</div>

	<!-- Delete -->
	<div id="delModal" class="reveal-modal tiny" data-reveal>
		<p class="p-modal"><span id="hpid" class='hpid var-modal' style="display:none;"></span>Yakin ingin menghapus <span class="nama var-modal"></span> dari daftar jabatan?</p><br/><br/>
		<span id="loadingDel" style="display:none;">Sedang memproses..</span>
		<span id="errorDel" style="display:none;color:red;">Ma'af, gagal dihapus</span>
		<span class="pull-right"><button class="deleteUrl btn btn-xs tmb-utama" id="Hapus"> Yakin</button> <button class="btn tmb-kedua close-reveal-modal close-modal"> Tidak</button></span>
	</div>
	
    <div class="col-lg-12">
        
        <div class="panel panel-default kotakwidget">
			<div class="panel-heading kepalakotakwidget">
				Data Master Jabatan
				<button class="btn btn-sm tmb-tambah tmb-utama pull-right" data-reveal-id="addModal" data-reveal><i class="fa fa-plus"></i> Tambah</button>
			</div>
				
            <div class="panel-body">
				<div class="row">
					<div class="col-md-12">
						<div id="contentJabatan">
						<table class="table table-condensed">
						  <thead>
							<tr>
								<th>#</th>
								<th>Jabatan</th>
								<th></th>
							</tr>
						  </thead>
						  <tbody>
							<?php 
								if(count($list)>0){
									$i = 1; 
									foreach ($list->result() as $row) {
							?>
							<tr class="hover">
								<td width="30"><?php echo $i++ ?></td>
								<td><?php echo $row->jabatan;?></td> 
								<td class="text-right">											
									<a class="editorLink" data-reveal-id="delModal" data-reveal nama="<?php echo $row->jabatan; ?>" hpid="<?php echo $row->id_m_jabatan;?>"><span class="glyphicon glyphicon-floppy-remove"></span> Hapus</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
									<a onclick="edit_jabatan('<?php echo $row->id_m_jabatan;?>')" class="editorLink" data-reveal-id="editModal" data-reveal><span class="glyphicon glyphicon-edit"></span> Edit</a>
								</td>
							</tr>
							<?php
									}
								}else{
							?>
							<tr>
								<td colspan="3"><center>Belum ada data master Jabatan</center></td>
							</tr>
							<?php
								}
							?>
						  </tbody>
						</table>
						</div>
					</div>
				</div>
            </div>

        </div>

	</div>
	
</div>
<script>
// Auto Focus Input Form Master
$(document).on('opened', '[data-reveal]', function () {
	$('.clearable').focus();	
	$('.editFocus').focus();
	$(".editFocus").val($(".editFocus").val());	
});
$(document).on('closed', '[data-reveal]', function () {
	$('#errorAdd').hide();
	$('#errorDel').hide();
});	
// Save
$('#SaveJabatan').bind('submit', function(event) {
	var link = $(this).attr('action');
	$.ajax({
		url: link,
		type: "POST",
		data: $(this).serialize(),
		cache: false,
		beforeSend: function(){
			$('#errorAdd').hide();
			$('#loadingAdd').show();
		},          
		success: function() {
			var $container = $("#contentJabatan");
			$container.load("<?php echo site_url(); ?>m_jabatan/refresh");
			
			$('#loadingAdd').hide();
			$('#addModal').foundation('reveal', 'close');
		},
		error:function(){
			$('#loadingAdd').hide();
			$('#errorAdd').show();
		}
	});
	return false; // dont post it automatically
});
// Edit
function edit_jabatan(id_m_jabatan){
	$.ajax({
		type: "POST",
		url : "<?php echo site_url(); ?>m_jabatan/edit",
		data: "id_m_jabatan="+id_m_jabatan,
		cache: false,
		success: function(response) {
			$('#edit').html(response);
		}
	});
	return false;
}
// Update
$(document).ajaxComplete(function(){
	$('#UpdateJabatan').bind('submit', function(event) {
		var link = $(this).attr('action');
		$.ajax({
			url: link,
			type: "POST",
			data: $(this).serialize(),
			cache: false,
			beforeSend: function(){
				$('#loadingUpdate').show();
			},          
			success: function() {
				var $container = $("#contentJabatan");
				$container.load("<?php echo site_url(); ?>m_jabatan/refresh");
				
				$('#loadingUpdate').hide();
				$('#editModal').foundation('reveal', 'close');
			},
			error:function(){
				$('#loadingUpdate').hide();
				$('#errorUpdate').show();
			}
		});
		return false; // dont post it automatically
	});
});
// Delete
$("#Hapus").on("click", function(){
	var id_m_jabatan = $("#hpid").text();
	$.ajax({
		type: "POST",
		data: "id_m_jabatan="+id_m_jabatan,
		url : "<?php echo site_url(); ?>m_jabatan/delete",				
		cache: false,
		beforeSend: function(){
			$('#loadingDel').show();
			$('#errorDel').hide();
		}, 
		success: function(){
			var $container = $("#contentJabatan");
			$container.load("<?php echo site_url(); ?>m_jabatan/refresh");
			
			$('#loadingDel').hide();
			$('#delModal').foundation('reveal', 'close');
		},
		error:function(){
			$('#loadingDel').hide();
			$('#errorDel').show();
		}
	});
	return false; 
});
</script>